<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item label="视频名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button plain type="success" @click="batchRevert">
          <Icon class="mr-5px" icon="material-symbols:download" />
          还原
        </el-button>
        <el-button plain type="primary" @click="setClearDays" icon="Edit">
          设置定期清理天数
        </el-button>
        <el-button @click="retreat" type="primary" style="float: right">返回</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 视频列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" label="id" align="center" key="id" prop="id" />
      <el-table-column label="ID" align="center" prop="id" :width="60" />
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column prop="source" label="来源" align="center" />
      <el-table-column prop="author" label="作者" align="center" />
      <el-table-column prop="duration" label="时长" align="center">
        <template #default="scope"> {{ scope.row.duration }}(秒) </template>
      </el-table-column>
      <el-table-column prop="ext" label="格式" align="center" />
      <el-table-column prop="status" label="状态" align="center">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.VIDEO_TRANSCODER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="openVideoUpload('deleted', scope.row.id)">
            查看
          </el-button>
          <el-button link type="success" @click="revert(scope.row.id)"> 还原 </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
  <update ref="updateRef" />

  <!-- 设置定期清理天数对话框 -->
  <ClearDaysForm ref="clearDaysFormRef" @success="getList" />
</template>
<script lang="ts" setup>
import * as videoApi from '@/api/video'
import update from '@/views/video/update.vue'
import { DICT_TYPE } from '@/utils/dict'
import router from '@/router'
import ClearDaysForm from './ClearDaysForm.vue'
//视频库-回收站列表
defineOptions({ name: 'VideoRecycle' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const queryFormRef = ref() // 搜索的表单
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null,
  type: 10,
  createTime: [],
  deleted: 1,
  animation: true
})

const updateRef = ref<any>()

const openVideoUpload = (type: string, id?: number) => {
  updateRef.value.openDialog(type, id)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

// 设置定期清理天数
const clearDaysFormRef = ref()
const setClearDays = async () => {
  clearDaysFormRef.value.open(routeInfo.meta.title, queryParams.animation)
}

// 返回按钮
const retreat = () => {
  router.go(-1)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    let ids = []
    ids.push(id)
    // 发起删除
    await videoApi.deleteByIds(ids)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const batchRevert = async () => {
  if (selectedItems.value.length == 0 || selectedItems.value.length > 10) {
    ElMessage.warning('请选择要还原的数据!')
    return
  }
  let ids = []
  selectedItems.value.forEach((res) => {
    ids.push(res.id)
  })
  await videoApi.revert(ids)
  await getList()
}

//还原资源
const revert = async (id) => {
  await message.confirm('确定还原资源吗？')
  let ids = []
  ids.push(id)
  await videoApi.revert(ids)
  await getList()
}
const selectedItems = ref([])
/** 多选 */
const handleSelectionChange = (val) => {
  selectedItems.value = val
}
/** 查询列表 */
const routeInfo = useRoute()
const getList = async () => {
  loading.value = true
  console.log(routeInfo)
  // 路由是否为动画库
  queryParams.animation = routeInfo.name === 'animationRecycle' ? true : false
  try {
    const data = await videoApi.recycle(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
